package com.bbs.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bbs.entity.Collection;
import com.bbs.entity.User;
import com.bbs.service.CollectionService;
import com.bbs.util.Page;

public class CollectionServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_method");
		if("findMine".equals(method)){
			findMine(request,response);
		}
	}

	private void findMine(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		User user = (User) request.getSession().getAttribute("user");
		if(user == null){
			response.sendRedirect("index.jspx?_method=login");
		} else {
			String p = request.getParameter("page");
			Page page = new Page();
			page.setTotleSize(new CollectionService().getCountByuid(user.getId()));
			if(p != null){
				page.setCurrentPage(Integer.parseInt(p));
			}
			List<Collection> clist = new CollectionService().findMine(user.getId(),page);
			if(clist == null){
				clist = new ArrayList<Collection>();
			}
			request.getSession().setAttribute("collectionlist", clist);
			request.getSession().setAttribute("page", page);
			response.sendRedirect("index.jspx?_method=usercenter");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
